from api.base.sys.auth.queryRolePage import QueryRolePage
from tools.utils import do_rest


class SaveOrUpdate():
    """创建新用户的接口"""

    def __init__(self):
        self.url = 'https://xxxxxx/xx'

    def save(self, shortName, opName, emailAdress, mobileNo=None, imAccount=None, notes=None, org=None, *roles):
        """创建新用户"""
        role_list = []
        for role in roles:
            role_dict = {}
            bquery_role_id = QueryRolePage().query_role_id(roleName=role)
            role_dict["id"] = bquery_role_id
            role_dict["roleName"] = role
            role_list.append(role_dict)

        data = {"op": {"opKind": 2,
                       "loginCode": shortName,
                       "opCode": shortName,
                       "shortName": shortName,
                       "opName": opName,
                       "emailAdress": emailAdress,
                       "mobileNo": mobileNo,
                       "imAccount": imAccount,
                       "notes": notes,
                       "loginPasswd": "yMQvN2GlHIWLATwU/N7a4Q=="}, "roles": role_list, "org": org}
        re = do_rest(self.url, param=data, method='post')
        return re.json()
